package cam;
/*
 * webcam.java
 *
 * Created on 19 de Março de 2008, 11:38
 */

/**
 *
 * @author  contpedroh
 */

import cam.*;
import java.applet.AppletContext;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class webcam extends javax.swing.JApplet {
    
    JWebCam myWebCam = null;
    
    /** Initializes the applet webcam */
    public void init() {
        
   
    
        
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        try
        {
           myWebCam = new JWebCam ( "TimeSlice Web Cam Capture" );
          //  myWebCam.setVisible ( true );
 
            if ( !myWebCam.initialise() )
            {
              //  System.out.println ("Web Cam not detected / initialised");
            }
            else{
                
                jPanel1.add(myWebCam.visualComponent);
                jPanel1.setSize(640,480);
              //  jPanel1.setVisible(true);
              //  jPanel1.setEnabled(true);
                //jLabel1.setText("Ola");
                repaint();
                String tipo_foto=getParameter("tipo_foto");
                String action_php=getParameter("action_php");
                myWebCam.action_php=action_php;
                myWebCam.tipo_foto=tipo_foto;
            }
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
    }
    
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jPanel1.setBorder(new javax.swing.border.MatteBorder(null));
        jPanel1.setMinimumSize(new java.awt.Dimension(640, 480));
        jPanel1.setPreferredSize(null);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 633, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 294, Short.MAX_VALUE)
        );

        jButton2.setText("Capturar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 635, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(226, 226, 226)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(130, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        myWebCam.meuap=this;
        myWebCam.captura();
        //fecharJanela();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        myWebCam.captura(); 
       // fecharJanela();// TODO add your handling code here:
    }//GEN-LAST:event_formKeyPressed
    

        public void fecharJanela(){
           
            myWebCam.playerClose();
            
                try {
            //    url = new URL("window.close();");
            AppletContext browser = getAppletContext();
            browser.showDocument(new URL("javascript:window.close();"));
            //JOptionPane.showMessageDialog(null, "Fechou");
        } catch (MalformedURLException ex) {
            JOptionPane.showMessageDialog(null,"Não foi possível fechar a janela!Favor fecha-la manualmente.");
            Logger.getLogger(JWebCam.class.getName()).log(Level.SEVERE, null, ex);
        }
        //JOptionPane.showMessageDialog(null, "Fechou");

        //JOptionPane.showMessageDialog(null, "Fechou");
    }
        
        
  public void finalizaPlayer(){
           
            myWebCam.playerClose();
           
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    
}
